<?php
namespace Admin\Controller;
use Admin\Controller\AdminController;
use Admin\Model\ArticleSortModel;
use Admin\Model\ArticleModel;
class ArticleController extends AdminController
{
    //文章列表$this->checkPower();
    public function index ()
    {
        $this->checkPower();
        //根据作者名搜索
        $name = "";
        if(!empty($_GET['name'])){
            $name = I("get.name");
        }
        //根据文章分类
        $sort = "";
        if(!empty($_GET['sort'])){
            $sort = I("get.sort");
//            echo $sort;die;
        }
        //根据文章发布时间搜索
        $time= "";
        if(!empty($_GET['time'])){
            $time = I("get.time");
        }
        //根据文章是否显示搜索
        $is_show = "";
        if(!empty($_GET['is_show'])){
            $is_show = I("get.is_show");
        }
        $data['name'] = $name;
        $data['sort'] = $sort;
        $data['time'] = $time;
        $data['is_show'] = $is_show;

        //排序字段
        $order = "";
        if(!empty($_GET['order'])){
            $order = I("get.order");
        }
        //排序方式
        $order1 = "";
        if(!empty($_GET['order1'])){
            $order1 = I("get.order1");
        }
        $data['order'] = $order;
        $data['order1'] = $order1;
        $this->title = "文章列表";

        $art = new ArticleModel();
        $res = $art -> queryAllArt($data);
        $this->artList = $res['list'];
        $this->page = $res['show'];

        //文章分类
        $sort = new ArticleSortModel();
        $sortList = $sort->queryAllSort();
        $this->sortList = $sortList;

        $this->display("Article/index");
    }
    //文章分类列表$this->checkPower();
    public function sort ()
    {
        $this->checkPower();
        $this->title = "文章分类列表";
        $sort = new ArticleSortModel();
        $sortList = $sort->queryAllSort();
        $this->sortList = $sortList;
        $this->display("Article/sort");
    }

    //更改分类是否可用
    public function isUse()
    {
        $id = I("get.id");
        $is_use = I("get.use") == 0 ? 1 : 0;
        $sort = new ArticleSortModel();
        $data['is_use'] = $is_use;
        $sort->updateSort($id,$data);
        $this->redirect("Article/sort");
    }

    //更改文章是否显示$this->checkPower();
    public function isShow ()
    {
//        $this->checkPower();
        $id = I("get.id");
        $is_show = I("get.show") == 0 ? 1 : 0;
        $art = new ArticleModel();
        $art->updateShow($id,$is_show);
        $this->redirect("Article/index");
    }

    //根据文章id查看文章详情
    public function queryArtInfo ()
    {
        $id = I("get.id");
        $this->title = "文章详情";
        $art = new ArticleModel();
        $info = $art->queryArtInfoById($id);
        $this->info = $info;
        $this->assign("prev",$_SERVER['HTTP_REFERER']);
        $this->display("Article/articleDetail");
    }

    //编辑分类名
    public function editSort()
    {
        $this->checkPower();
        $name =  $_GET['name'];
        $id = $_GET['id'];
        $sort = new ArticleSortModel();
        $data['name'] = $name;
        echo $sort->updateSort($id,$data);
    }

    //支部动态发布$this->checkPower();
    public function addArt()
    {
        $this->checkPower();
        $this->title = "支部动态文章发布";
        $this->assign("prev",$_SERVER['HTTP_REFERER']);
        $this->display("Article/add");
    }
    //支部动态文章保存
    public function saveArt ()
    {
//        var_dump($_FILES);die;
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Article/'; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传（子）目录
        // 上传文件
        $info   =   $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }
        $imageName = $info['photo']['savename'];
        $url = $this->get_img_url($_SERVER['REQUEST_SCHEME']."://".$_SERVER['HTTP_HOST']."/dangjian/Article/", $imageName, 0);

        $data['sort_id'] = 4;
        $data['user_id'] = $_SESSION['admin']['id'];
        $data['publish_time'] = date("Y-m-d H:i:s");
        $data["title"] = I("post.title");
        $data["is_show"] = 1;
        $data["content"] = I("post.content");
        $data["description"] = I("post.description");
        $data["thumbnail"] = $url;

        $art = new ArticleModel();
        $res = $art -> saveArt($data);
        if($res > 0){
            $this->redirect("Article/index");
        }else{
            $this->error("发布失败");
        }
    }

    //两学一做发布$this->checkPower();
    public function addArtc()
    {
        $this->checkPower();
        $this->title = "两学一做文章发布";
        //查询所有类别
        $sort = new ArticleSortModel();
        $sortList = $sort->queryAllSort();
        unset($sortList['3']);
        $this ->sortList = $sortList;
        $this->assign("prev",$_SERVER['HTTP_REFERER']);
        $this->display("Article/adds");
    }
    //两学一做保存
    public function saveArtc ()
    {
        $data['sort_id'] = I("post.sort");
        $data['user_id'] = $_SESSION['admin']['id'];
        $data['publish_time'] = date("Y-m-d H:i:s");
        $data["title"] = I("post.title");
        $data["is_show"] = 1;
        $data["content"] = I("post.content");
        $data["description"] = I("post.description");

        $art = new ArticleModel();
        $res = $art -> saveArt($data);
        if($res > 0){
            $this->redirect("Article/index");
        }else{
            $this->error("发布失败");
        }
    }
}